package com.ylb.rent.controller;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ylb.rent.config.PageConfig;
import com.ylb.rent.entity.Country;
import com.ylb.rent.entity.School;
import com.ylb.rent.util.EsUtil;
import com.ylb.rent.util.R;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;

import com.ruoyi.common.enums.BusinessType;
import  com.ylb.rent.entity.City;
import com.ylb.rent.service.ICityService;


/**
 * 城市Controller
 * 
 * @author ylb
 * @date 2023-06-29
 */
@RestController
@RequestMapping("/city/city")
public class CityController
{
    @Autowired
    private ICityService cityService;

    @Autowired
    RedisTemplate<String,Object> redisTemplate;


    @Autowired
    EsUtil esUtil;
    /**
     * 查询城市列表
     */
    @PreAuthorize("@ss.hasPermi('city:city:list')")
    @GetMapping("/list")
    public String list(City city, PageConfig pageConfig)
    {
        PageHelper.startPage(pageConfig);
        List<City> list = cityService.selectCityList(city);
        return R.successJSONData(new PageInfo<>(list));

    }


    @GetMapping("/search/keyword")
    public String searchByKeyword(String keyword)
    {
        List<City> cityName = esUtil.keywordSearch("cityName", keyword, City.class);

        return R.successJSONData(cityName);

    }

    @GetMapping("/list/hot")
    public String hotList()
    {
        Set<Object> hotCity = redisTemplate.opsForSet().members("hotCity");
        return R.successJSONData(hotCity);

    }


    /**
     * 获取城市详细信息
     */
    @PreAuthorize("@ss.hasPermi('city:city:query')")
    @GetMapping(value = "/{id}")
    public String getInfo(@PathVariable("id") Long id)
    {
        return R.successJSONData(cityService.selectCityById(id));
    }

    /**
     * 新增城市
     */
    @PreAuthorize("@ss.hasPermi('city:city:add')")
    @Log(title = "城市", businessType = BusinessType.INSERT)
    @PostMapping
    public String add(@RequestBody City city)
    {
        return R.successJSONData(cityService.insertCity(city));
    }

    /**
     * 修改城市
     */
    @PreAuthorize("@ss.hasPermi('city:city:edit')")
    @Log(title = "城市", businessType = BusinessType.UPDATE)
    @PutMapping
    public String edit(@RequestBody City city)
    {
        return R.successJSONData(cityService.updateCity(city));
    }

    /**
     * 删除城市
     */
    @PreAuthorize("@ss.hasPermi('city:city:remove')")
    @Log(title = "城市", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public String remove(@PathVariable Long[] ids)
    {
        return R.successJSONData(cityService.deleteCityByIds(ids));
    }
}
